package com.vastcom.invoker.action;


import com.vastcom.invoker.dto.UserInfo;
import com.vastcom.invoker.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.MediaType;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.*;

import javax.jms.Queue;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
public class PublicAction {

    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private ProviderService providerService;

    @Autowired
    private Queue queue;

    @Autowired
    private JmsTemplate jmsTemplate;


    @GetMapping(value = "callTest",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<ServiceInstance> callTest() throws Exception{
        return this.discoveryClient.getInstances("FIRSTPROVIDER");
    }

    @GetMapping("getProviderDataTest")
    @ResponseBody
    public HashMap getProviderDateTest() throws Exception{

        return providerService.getObjectTest();
    }

    @PostMapping("postProviderTest")
    @ResponseBody
    public HashMap postProviderTest(@RequestBody UserInfo userInfo) throws Exception{

        return providerService.postObject(userInfo);

    }

    @GetMapping("sendRemoteActivityMessageTest")
    @ResponseBody
    public HashMap sendRemoteActivityMessageTest() throws Exception{
        HashMap m=new HashMap();

        com.vastcom.provider.model.UserInfo u=new com.vastcom.provider.model.UserInfo();
        u.setId(UUID.randomUUID().toString());
        u.setLastlogin(new Timestamp(System.currentTimeMillis()));
        u.setPassword("********12");
        u.setUsername("I'm a Remote Message");
        jmsTemplate.convertAndSend(queue,u);

        return m;
    }



}
